package com.zkingsoft.rhi.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelics;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelicsPublication;
import com.zkingsoft.rhi.handler.Result;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsPublicationService;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsService;
import com.zkingsoft.rhi.utils.MyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xieguo
 * @since 2020-11-17
 */
@Api(tags = "不可移动文物公布时间相关")
@RestController
@RequestMapping("/rhi/publication")
public class ImmovableHistoricalRelicsPublicationController {

    @Autowired
    ImmovableHistoricalRelicsPublicationService ihrps;

    @Autowired
    ImmovableHistoricalRelicsService ihrs;

    @Autowired
    MyUtils myUtils;

    /**
     * @author xieguo
     * 功能描述：新增公布时间相关
     * @param ihrp
     * @return com.alibaba.fastjson.JSONObject
     * @date 2020/11/17 17:06
     */
    @ApiOperation(value = "新增公布时间相关")
    @PostMapping("/insert")
    public JSONObject insertPublication(@RequestBody ImmovableHistoricalRelicsPublication ihrp){
        if (null==ihrp.getPublicationNumber()){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"参数错误");
        }
        QueryWrapper<ImmovableHistoricalRelicsPublication> wrapper = new QueryWrapper<>();
        wrapper.eq("publication_number",ihrp.getPublicationNumber());
        ImmovableHistoricalRelicsPublication one = ihrps.getOne(wrapper);
        if (ObjectUtil.isEmpty(one)){
            boolean save = ihrps.save(ihrp);
            if (save){
                return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS,"保存成功");
            }else {
                return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"保存失败");
            }
        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"公布文号不能重复");
        }
    }

    /**
     * @author xieguo
     * 功能描述：删除公布时间相关
     * @param ids
     * @return com.alibaba.fastjson.JSONObject
     * @date 2020/11/17 17:06
     */
    @ApiOperation(value = "删除公布时间相关")
    @GetMapping("/delete/{ids}")
    public JSONObject deletePublication(@PathVariable("ids") String ids){
        List<Integer> idList = myUtils.handleIds(ids);
        QueryWrapper<ImmovableHistoricalRelicsPublication> wrapper = new QueryWrapper<>();
        wrapper.in("id",idList);
        ArrayList<String> strings = new ArrayList<>();
        List<ImmovableHistoricalRelicsPublication> list = ihrps.list(wrapper);
        list.forEach(data->{
            strings.add(data.getPublicationNumber());
        });
        QueryWrapper<ImmovableHistoricalRelics> immovableHistoricalRelicsQueryWrapper = new QueryWrapper<>();
        immovableHistoricalRelicsQueryWrapper.in("publication_number",strings);
        List<ImmovableHistoricalRelics> immovableHistoricalRelicsList = ihrs.list(immovableHistoricalRelicsQueryWrapper);
        if (ObjectUtil.isNotEmpty(immovableHistoricalRelicsList)){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"有关联数据，需要先删除关联数据在进行删除");
        }
        boolean delete = ihrps.removeByIds(idList);
        if (delete){
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS,"删除成功");
        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"删除失败");
        }
    }

    /**
     * @author xieguo
     * 功能描述：修改公布时间相关
     * @param ihrp
     * @return com.alibaba.fastjson.JSONObject
     * @date 2020/11/17 17:06
     */
    @ApiOperation(value = "修改公布时间相关")
    @PostMapping("/update")
    public JSONObject updatePublication(@RequestBody ImmovableHistoricalRelicsPublication ihrp){
        if (null==ihrp.getPublicationNumber()||null==ihrp.getId()){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"参数错误");
        }

        ImmovableHistoricalRelicsPublication oldIhrp = ihrps.getById(ihrp.getId());

        if (!ihrp.getPublicationNumber().equals(oldIhrp.getPublicationNumber())){
            QueryWrapper<ImmovableHistoricalRelicsPublication> wrapper = new QueryWrapper<>();
            wrapper.eq("publication_number",ihrp.getPublicationNumber());
            ImmovableHistoricalRelicsPublication one = ihrps.getOne(wrapper);
            if (ObjectUtil.isNotEmpty(one)){
                return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"公布文号不能重复");
            }
        }

        boolean update = ihrps.updateById(ihrp);
        if (update){
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS,"修改成功");
        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"修改失败");
        }
    }

    /**
     * @author xieguo
     * 功能描述：查询公布时间相关，可搜索
     * @param jo
     * @return com.alibaba.fastjson.JSONObject
     * @date 2020/11/17 17:07
     */
    @ApiOperation(value = "查询公布时间相关，可搜索")
    @PostMapping("/query")
    public JSONObject queryPublication(@RequestBody(required = false) JSONObject jo){
        Integer limit=null;
        Integer offset=null;
        String publicationNumber=null;
        String level=null;
        String publicationBatch = null;

        if (ObjectUtil.isNotEmpty(jo)){
            publicationBatch= jo.getString("publicationBatch");
            limit = (Integer) jo.get("limit");
            offset = (Integer) jo.get("offset");
            publicationNumber = (String) jo.get("publicationNumber");
            level = jo.getString("level");
        }


        if (offset==null||limit==null){
            offset=1;
            limit=10000;
        }

        Page<ImmovableHistoricalRelicsPublication> ihrpPage = new Page<>(offset, limit);
        QueryWrapper<ImmovableHistoricalRelicsPublication> wrapper = new QueryWrapper<>();
            if (null != publicationNumber) {
                wrapper.like("publication_number", publicationNumber);
            }
            if (null!=level){
                wrapper.eq("level",level);
            }
            if (null!=publicationBatch){
                wrapper.like("publication_batch",publicationBatch);
            }
            IPage<ImmovableHistoricalRelicsPublication> page = ihrps.page(ihrpPage, wrapper);
            Integer total = (int) page.getTotal();
            return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS, "查询成功", page.getRecords(), total);

    }


    @ApiOperation(value = "回显")
    @GetMapping("/selectPublicationByIds/{id}")
    public JSONObject selectPublicationById(@PathVariable String id) {
        ImmovableHistoricalRelicsPublication historicalRelics = ihrps.getById(id);
        return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS, "查询成功", historicalRelics, null);
    }
}

